package com.chucang.shucang.ttp.listener;

import cn.hutool.core.bean.BeanUtil;
import com.chucang.shucang.common.mq.constant.RabbitConstant;
import com.chucang.shucang.common.mq.to.NFTDestroyTO;
import com.chucang.shucang.ttp.nftcenter.model.dto.NFTOperateDTO;
import com.chucang.shucang.ttp.nftcenter.model.req.NFTDestroyReq;
import com.chucang.shucang.ttp.nftcenter.proxy.AvataProxy;
import com.rabbitmq.client.Channel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author flitsneak
 * @email flitsneak@gmail.com
 * @date 2022/10/17 20:22
 * @description nft销毁
 */
@Slf4j
@Component
@RequiredArgsConstructor
@RabbitListener(queues = RabbitConstant.NFT_DESTROY_NFT_QUEUE)
public class NFTDestroyListener {
    private final AvataProxy avataProxy;

    @RabbitHandler
    public void nftPublishListener(NFTDestroyTO nftDestroyTO, Channel channel, Message message) throws IOException {
        log.info("开始销毁藏品！");
        try {
            NFTDestroyReq nftDestroyReq = new NFTDestroyReq();
            BeanUtil.copyProperties(nftDestroyTO, nftDestroyReq);
            NFTOperateDTO nftOperateDTO = avataProxy.destroyNFT(nftDestroyReq);
            log.info("藏品销毁结果====》{}", nftOperateDTO);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error("销毁藏品异常！");
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
